package com.baiyang.server.service.system.impl;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baiyang.server.dao.system.UserRoleDao;
import com.baiyang.server.model.system.UserRole;
import com.baiyang.server.service.impl.BaseServiceImpl;
import com.baiyang.server.service.system.UserRoleService;
import com.baiyang.server.tools.DataTool;
import org.springframework.transaction.annotation.Transactional;

/**
 * 
 * @author XVX
 * BY: @Primary 注解帮助生命@Autowired优先注入的实列
 */
@Service	
public class UserRoleServiceImpl extends BaseServiceImpl<UserRoleDao, UserRole>implements UserRoleService{
	@Autowired
	private UserRoleDao userRoleDao = null;
	
	@Override
	public List<UserRole> selectByUserId(String userId) {
		return userRoleDao.selectByUserId(userId);
	}
	
	/**
	 * @return userId == null?null:roleIds
	 */
	@Override
	public List<String> selectIdsByUserId(String userId) {
		if(DataTool.isBlank(userId)) {
			return null;
		}
		List<String> roleIds = userRoleDao.selectIdsByUserId(userId);
		return roleIds;
	}

	@Override
	@Transactional
	public void DelAndInsertBatch(String userId, List<UserRole> list) {
		UserRole userRole = new UserRole();
		userRole.setUserId(userId);
		userRoleDao.delete(userRole);
		if(!DataTool.isBlank(list)){
			list.forEach(e->{
				e.setId(DataTool.createUUID());
			});
			userRoleDao.insertBatch(list);
		}
	}
}
